/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswing;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import spagcore.SpaglCoreCombo;
import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreFont;

/**
 * A JComboBox (javax.swing.JComboBox) that conforms to the SpaglCoreList interface
 * @author Sam Pottinger
 */
public class SpaglCombo extends JComboBox implements SpaglCoreCombo{

	private static final long serialVersionUID = 1L;
	private DefaultComboBoxModel model;
	private SpaglCoreContainer parentContainer;
	private SpaglCoreFont widgetFont;

	/**
	 * Creates a bare bone/empty combo box
	 * @param targetContainer The parent {@link SpaglContainer} for this widget
	 */
	public SpaglCombo(SpaglCoreContainer targetContainer)
	{
		super(new DefaultComboBoxModel());
		model = (DefaultComboBoxModel)super.getModel();
		parentContainer = targetContainer;
	}
	
	/**
	 * Creates a combo box with specified placement parameters for the layout manager
	 * 
	 * @param targetContainer The parent {@link SpaglContainer} for this widget
	 * @param placementParameters The placement parameters for the layout manager
	 */
	public SpaglCombo(SpaglCoreContainer targetContainer, String placementParameters)
	{
		super(new DefaultComboBoxModel());
		model = (DefaultComboBoxModel)super.getModel();
		parentContainer = targetContainer;
		parentContainer.placeWidget(this, placementParameters);
	}
	
	/**
	 * Adds an element to the combo box
	 */
	public void addElement(Object newElement) {
		model.addElement(newElement);
	}

	/**
	 * Removes all elements from this combo box
	 */
	public void removeAllElements() {
		model.removeAllElements();
	}

	/**
	 * Removes an element at a given position
	 * 
	 * @param i the index of the element to remove
	 */
	public void removeElement(int i) {
		model.removeElementAt(i);
	}

	/**
	 * Sets the combo box to a given array of objects
	 * 
	 * @param newElementList an array of objects that will be associated with this widget
	 */
	public void setElementList(Object[] newElementList) {
		this.removeAllElements();
		for(Object o : newElementList)
			this.addElement(o);
	}
	
	/**
	 * Gets the container for this widget
	 * 
	 * @return the {@link SpaglContainer} for this widget
	 */
	public SpaglCoreContainer getContainer() {
		return parentContainer;
	}

	/**
	 * Gets the font associated with this widget
	 * 
	 * @return The {@link SpaglFont} associated with this object
	 */
	public SpaglCoreFont getWidgetFont() {
		return widgetFont;
	}

	/**
	 * Sets the font for this widget
	 * 
	 * @param newFont the font to be attached with this combo box
	 */
	public void setFont(SpaglCoreFont newFont) {
		super.setFont((SpaglFont)newFont);
	}
	
	/**
	 * Sets the font to be attached with this combo box while simultating disposal
	 * 
	 * @param newFont the font to be attached with this combo box
	 * @param dispose This is ignored by swing, for cross library purposes this would (if true) dipose the resource instantly
	 */
	public void setFont(SpaglCoreFont newFont, boolean dispose) {
		if(!dispose)
			widgetFont = newFont;
		else
			widgetFont = null;
		super.setFont((SpaglFont)newFont);
	}
	
	/**
	 * Swing does not need manual disposal, so this is here just for cross library purposes
	 */ 
	public void dispose() {}
	
	/**
	 * Adds an event listener to this combo box
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param targetObject The object where the method should be fired
	 */
	public void addEventListener(int eventID, String method, Object targetObject) {
		try{
			switch(eventID)
			{
				case SpaglEvent.SELECTION_CHANGED_EVENT: {this.addItemListener(new SpaglEventListener(targetObject.getClass().getMethod(method, SpaglCoreEvent.class), targetObject, SpaglEvent.SELECTION_CHANGED_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}
}
